﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

public partial class UI_TrailList : System.Web.UI.Page
{
    private IList<Bintje.Trail> foundTrails;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString.Count > 0)
        {
            bindList();
            hprViewmap.NavigateUrl = Request.Url.ToString().Replace("TrailList.aspx", "mapsearch.aspx");
            lblshowCount.Text = string.Format("page 1 of {0}", (((int)ViewState["TrailCount"]) / dlTrailList.PageSize) + 1);
        }
    }
 
    public List<int> getTrailTags()
    {
        List<int> lstTags = new List<int>();
        return lstTags;
    }

    private void bindList()
    {
        try
        {
            SearchTrail();
            if (null != foundTrails && foundTrails.Count() > 0)
            {
                int userId = AppUser.currUser();
                string userName = AppUser.getUsernameById(userId);
                var tra = (from t in foundTrails
                           orderby t.Id descending
                           select new
                           {
                               ID = t.Id,
                               Name = t.Title,
                               Information = t.Title,
                               LocationCountry = (t.Waypoints != null && t.Waypoints.Count > 0) ? t.Waypoints[0].Country : string.Empty,
                               TrailComments = t.Annotations != null ? t.Annotations.Count : 0,
                               //ImageUrl = t.Waypoints.Count > 0 && t.Waypoints[0].ImageUrl != null && t.Waypoints[0].ImageUrl.Length > 0 && !string.IsNullOrEmpty(t.Waypoints[0].ImageUrl[0]) ? t.Waypoints[0].ImageUrl[0] : "~/sitefiles/Get-Thumbnail.aspx?f=~/images/no_image.gif&w=50&h=50",
                               TripStory = t.Description,
                               UserID = userId,
                               username = userName
                           }).ToArray();

                dlTrailList.DataSource = tra;
                dlTrailList.DataBind();
                lblMsg.Visible = false;
                ViewState["TrailCount"] = foundTrails.Count();
            }
            else
            {
                lblMsg.Visible = true;
                ViewState["TrailCount"] = 0;
            }
        }
        catch (Exception)
        {
            lblMsg.Visible = true;
            ViewState["TrailCount"] = 0;
        }
    }

    private void SearchTrail()
    {
        /*Bintje.TrailFilter filter = null;
        
        if (null != Session["Filter"] && Session["Filter"] is Bintje.TrailFilter)
        {
            filter = Session["Filter"] as Bintje.TrailFilter;
        }
        else if (Request["catid"] == null && Request["keywords"] != null)
        {
            string trailName = Request.QueryString["keywords"];
            filter = new Bintje.TrailFilter();
            filter.TrailTitle = trailName;
        }*/
        if (Request["keywords"] != null)
        {
            string filter = Request.QueryString["keywords"];
            foundTrails = Bintje.Trail.Search(filter);
        }
    }

    protected void dlTrailList_databound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataList lstImages = (DataList)e.Row.FindControl("lstImages");
            Literal ltrImages = (Literal)e.Row.FindControl("ltrImages");
            
            int trid = 0;
            if (null != foundTrails && int.TryParse(ltrImages.Text, out trid) && trid > 0)
            {
                Bintje.Trail trail = foundTrails.Single(t => t.Id == trid); 
                if (null != trail && null != trail.Waypoints && trail.Waypoints.Count > 0)
                {
                    int k = 0;
                    object[] imgLst = (object[])Array.CreateInstance(typeof(object), 3);
                    for (int i = 0; i < trail.Waypoints.Count; i++)
                    {
                        if (k > 2)
                            break;
                        if (null != trail.Waypoints[i].ImageUrl && trail.Waypoints[i].ImageUrl.Length > 0)
                        {
                            for (int j = 0; j < trail.Waypoints[i].ImageUrl.Length; j++)
                            {
                                imgLst[k] = new { ID = trail.Id, Picture = trail.Waypoints[i].ImageUrl[j] };
                                k++;
                                if (k > 3)
                                    break;
                            }
                        }
                    }
                    
                    lstImages.DataSource = imgLst;//.Take(3);
                    lstImages.DataBind();
                }
            }
        }
    }

    protected void lstImages_databound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Image imgTrial = (Image)e.Item.FindControl("imgTrial");
            if (imgTrial.ImageUrl == "~/sitefiles/Get-Thumbnail.aspx?f=~/Upload/Marker/&w=75&h=75" || imgTrial.ImageUrl == "")
            {
                imgTrial.Visible = false;
            }
        }
    }

    protected void hprDateAdded_Click(object sender, EventArgs e)
    {
        if (Request["catid"] == null && Request["keywords"] != null)
        {
            string trailName = Request.QueryString["keywords"];
            /*Bintje.TrailFilter filter = new Bintje.TrailFilter();
            filter.TrailTitle = trailName;*/
            IList<Bintje.Trail> t = Bintje.Trail.Search(trailName);
            dlTrailList.DataSource = t;
            dlTrailList.DataBind();

        }
    }

    protected void hprLocation_Click(object sender, EventArgs e)
    {
        SearchTrail();
        if (null != foundTrails && foundTrails.Count() > 0)
        {
            int userId = AppUser.currUser();
            string userName = AppUser.getUsernameById(userId);
            var tra = (from t in foundTrails
                       orderby t.Id descending
                       select new
                       {
                           ID = t.Id,
                           Name = t.Title,
                           Information = t.Title,
                           LocationCountry = (t.Waypoints != null && t.Waypoints.Count > 0) ? t.Waypoints[0].Country : string.Empty,
                           TrailComments = t.Annotations != null ? t.Annotations.Count : 0,
                           //ImageUrl = t.Waypoints.Count > 0 && t.Waypoints[0].ImageUrl != null && t.Waypoints[0].ImageUrl.Length > 0 && !string.IsNullOrEmpty(t.Waypoints[0].ImageUrl[0]) ? t.Waypoints[0].ImageUrl[0] : "~/sitefiles/Get-Thumbnail.aspx?f=~/images/no_image.gif&w=50&h=50",
                           TripStory = t.Description,
                           UserID = userId,
                           username = userName
                       }).ToArray().OrderBy(t => t.LocationCountry);

            dlTrailList.DataSource = tra;
            dlTrailList.DataBind();
        }
    }

    protected void hprTotalComments_Click(object sender, EventArgs e)
    {
        SearchTrail();
        if (null != foundTrails && foundTrails.Count() > 0)
        {
            int userId = AppUser.currUser();
            string userName = AppUser.getUsernameById(userId);
            var tra = (from t in foundTrails
                       orderby t.Id descending
                       select new
                       {
                           ID = t.Id,
                           Name = t.Title,
                           Information = t.Title,
                           LocationCountry = (t.Waypoints != null && t.Waypoints.Count > 0 ) ? t.Waypoints[0].Country : string.Empty,
                           TrailComments = t.Annotations != null ? t.Annotations.Count : 0,
                           //ImageUrl = t.Waypoints.Count > 0 && t.Waypoints[0].ImageUrl != null && t.Waypoints[0].ImageUrl.Length > 0 && !string.IsNullOrEmpty(t.Waypoints[0].ImageUrl[0]) ? t.Waypoints[0].ImageUrl[0] : "~/sitefiles/Get-Thumbnail.aspx?f=~/images/no_image.gif&w=50&h=50",
                           TripStory = t.Description,
                           UserID = userId,
                           username = userName
                       }).ToArray().OrderBy(t => t.TrailComments);

            dlTrailList.DataSource = tra;
            dlTrailList.DataBind();
        }
    }

    protected void hpyType_Click(object sender, EventArgs e)
    {
        if (Request["catid"] == null && Request["keywords"] != null)
        {
            string trailName = Request.QueryString["keywords"];
            /*Bintje.TrailFilter filter = new Bintje.TrailFilter();
            filter.TrailTitle = trailName;*/
            IList<Bintje.Trail> t = Bintje.Trail.Search(trailName);
        }
    }

    protected void dlTrailList_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.dlTrailList.PageIndex = e.NewPageIndex;
        if (ViewState["TrailCount"] != null)
        {
            lblshowCount.Text = string.Format("page {0} of {1}", (e.NewPageIndex + 1).ToString(), (((int)ViewState["TrailCount"]) / dlTrailList.PageSize) + 1);
        }
        bindList();
    }
}
